﻿@using SmartStore.Collections
@using SmartStore.Core.Domain.Security
@using Telerik.Web.Mvc.UI
@model CustomerRoleModel

@Html.ValidationSummary(true)
@Html.HiddenFor(model => model.Id)
@Html.SmartStore().TabStrip().Name("customer-role-edit").Style(TabsStyle.Material).Items(x =>
{
    x.Add().Text(T("Common.General").Text).Content(TabGeneral()).Selected(true);
    x.Add().Text(T("Admin.Configuration.ACL").Text).Content(TabPermissions());
    x.Add().Text(T("Admin.Customers").Text).Content(TabCustomers());
    
    EngineContext.Current.Resolve<IEventPublisher>().Publish(new TabStripCreated(x, "customer-role-edit", this.Html, this.Model));
})

@helper TabGeneral()
{
    <table class="adminContent">
        <tr>
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.Name)
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.Name)
                @Html.ValidationMessageFor(model => model.Name)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.SystemName)
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.SystemName)
                @Html.ValidationMessageFor(model => model.SystemName)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.Active)
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.Active)
                @Html.ValidationMessageFor(model => model.Active)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.FreeShipping)
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.FreeShipping)
                @Html.ValidationMessageFor(model => model.FreeShipping)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.TaxDisplayType)
            </td>
            <td class="adminData">
                @Html.DropDownListFor(model => model.TaxDisplayType, Model.TaxDisplayTypes, T("Common.PleaseSelect").Text)
                @Html.ValidationMessageFor(model => model.TaxDisplayType)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.TaxExempt)
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.TaxExempt)
                @Html.ValidationMessageFor(model => model.TaxExempt)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.IsSystemRole)
            </td>
            <td class="adminData">			
			    <div class="form-control-plaintext">
				    @T(Model.IsSystemRole ? "Admin.Common.Yes" : "Admin.Common.No")
			    </div>

                @Html.HiddenFor(model => model.IsSystemRole)
                @Html.ValidationMessageFor(model => model.IsSystemRole)
            </td>
        </tr>
    </table>
}

@helper TabPermissions()
{
    if (Model.Id > 0)
    {
        if (Model.PermissionTree != null && Model.PermissionTree.HasChildren)
        {
            <p>
			    <button id="ExpandCollapseAll" type="button" class="btn btn-sm btn-secondary">
				    <span>@T("Common.ExpandCollapseAll")</span>
			    </button>
            </p>
            <div id="permission-tree" class="tree">
                @PermissionNode(Model.PermissionTree)
            </div>
        }
        else
        {
	        <div class="alert alert-warning">
		        @T("Admin.System.Warnings.NoPermissionsDefined")
	        </div>
        }
    }
    else
    {
		<div class="alert alert-warning">
			@T("Admin.SaveBeforeEdit")
		</div>
    }
}

@helper TabCustomers()
{
    if (Model.Id > 0)
    {
        <div class="adminContent">
            <div class="adminRow">
                <div class="adminTitle">
                    @Html.SmartLabelFor(model => model.SelectedRuleSetIds)
                </div>
                <div class="adminData">
                    @Html.EditorFor(model => model.SelectedRuleSetIds)
                    @Html.ValidationMessageFor(model => model.SelectedRuleSetIds)
                </div>
            </div>
        </div>
        if (Model.SelectedRuleSetIds.Any())
        {
            <div class="alert alert-info">
                @T("Admin.CustomerRoleMapping.RoleMappingListDescription")
            </div>
        }
        @(Html.Telerik().Grid<CustomerRoleMappingModel>()
		    .Name("role-mapping-grid")
		    .ClientEvents(events => events.OnRowDataBound("onRoleMappingRowDataBound"))
		    .Columns(columns =>
		    {
			    columns.Bound(x => x.Email)
				    .ClientTemplate("<a href=\"" + Url.Content("~/Admin/Customer/Edit/") + "<#= CustomerId #>\"><#= Email #></a>");
			    columns.Bound(x => x.Username)
				    .Visible(Model.UsernamesEnabled);
			    columns.Bound(x => x.FullName);
			    columns.Bound(x => x.Active)
					.ClientTemplate(Html.SymbolForBool("Active"))
					.Centered();
                columns.Bound(x => x.CreatedOn);
                columns.Bound(x => x.LastActivityDate);
			    columns.Bound(x => x.IsSystemMapping)
					.ClientTemplate(Html.SymbolForBool("IsSystemMapping"))
					.Centered();
		    })
            .ToolBar(commands => commands.Template(RoleMappingsGridCommands))
		    .Pageable(settings => settings.PageSize(Model.GridPageSize).Position(GridPagerPosition.Both))
		    .DataBinding(dataBinding => dataBinding.Ajax().Select("CustomerRoleMappings", "CustomerRole"))
		    .PreserveGridState()
		    .EnableCustomBinding(true));
    }
    else
    {
		<div class="alert alert-warning">
			@T("Admin.SaveBeforeEdit")
		</div>
    }
}

<script>
    function onRoleMappingRowDataBound(e) {
		if (e.dataItem && !e.dataItem.Active) {
			$(e.row).find('td').wrapInner('<span class="muted" />');
		}
    }

    $(function () {
        // Init permission tree.
        $('#permission-tree').tree('init', {
            nodeState: 'on-off',
            stateTitles: [@T("Common.Allow").JsText, @T("Common.Deny").JsText, @T("Admin.Permissions.AllowInherited").JsText, @T("Admin.Permissions.DenyInherited").JsText]
        });

        // Expand\collapse all.
        $('#ExpandCollapseAll').on('click', function () {
            $('#permission-tree').tree('expandAll');
        });

        // Reapply rules.
        $('#btnReapplyRules').on('click', function () {
    		$(this).doPostData({ });
        });
	});
</script>

@helper RoleMappingsGridCommands(Grid<CustomerRoleMappingModel> grid)
{
    if (Model.ShowRuleApplyButton)
    {
	    <button type="button" id="btnReapplyRules" class="t-button t-grid-save-changes" data-url="@Url.Action("ApplyRules", new { id = Model.Id })" data-ask="@T("Admin.Common.AskToProceed")">
		    <i class="fa fa-play"></i>
		    <span>@T("Admin.Rules.ReapplyRules")</span>
	    </button>
    }
}

@helper PermissionNode(TreeNode<IPermissionNode> node)
{
    <ul>
        @foreach (var child in node.Children)
        {
            <li data-name="permission-@child.Value.PermissionRecordId" 
                data-value="@(child.Value.Allow.HasValue ? (child.Value.Allow.Value ? 2 : 1) : 0)" 
                data-label="@(child.GetMetadata<string>("DisplayName", false))"
                title="@child.Value.SystemName">
                @if (child.HasChildren)
                {
                    @PermissionNode(child)
                }
            </li>
        }
    </ul>
}